package com.cgnpc.cuddemo.workbench.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.controller.BaseController;
import com.cgnpc.cud.core.domain.AjaxResult;
//import com.cgnpc.cud.shiro.util.ContextHolder;
import com.cgnpc.cud.shiro.util.ContextHolder;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/******************************
 * 用途说明: 用户相关  前端控制器
 * 作者姓名: PXMWRYA
 * 创建时间: 2019/07/19 09:14
 ******************************/
@RestController
@RequestMapping("/user")
public class UserInfoController extends BaseController {

    @Autowired
    CurrentUser currentUser;

    /**********************************
    * 用途说明: 获取当前用户信息
    * 参数说明 request
    * 返回值说明:
    ***********************************/
    @PostMapping("getCurrentUser")
//    @UBA(module = "用户模块",action = "获取当前用户",channel = OperatorType.Page)
    public AjaxResult getCurrentUser(HttpServletRequest request) {
        try {
            String userInfo = currentUser.getNowUser();

            return getObject(userInfo);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 获取当前用户信息
     * 参数说明 request
     * 返回值说明:
     ***********************************/
    @PostMapping("getCurrentUserDetail")
//    @UBA(module = "用户模块",action = "获取当前用户名字",channel = OperatorType.Page)
    public AjaxResult getCurrentUserDetail(HttpServletRequest request) {
        try {
            UserInfo currentUserDetail = currentUser.getCurrentUserDetail();

            return getObject(currentUserDetail);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 获取当前用户信息
     * 参数说明 request
     * 返回值说明:
     ***********************************/
    @PostMapping("getUserRealName")
//    @UBA(module = "用户模块",action = "获取当前用户名字",channel = OperatorType.Page)
    public AjaxResult getUserRealName(HttpServletRequest request) {
        try {
            String userInfo = currentUser.getUserRealName();

            return getObject(userInfo);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 获取多个人员信息
    * 参数说明 usersMap
    * 返回值说明:
    ***********************************/
    @PostMapping("getUsersInfo")
//    @UBA(module = "用户模块",action = "获取多个人员信息",channel = OperatorType.Page)
    public AjaxResult getUsersInfo(@RequestBody Map usersMap) {
        try {
            String userIds = usersMap.get("userIds").toString();
            List userslist = currentUser.getUsersByUserCenter(userIds);
            return getObject(userslist);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
    * 用途说明: 切换用户
    * 参数说明 userId
    * 返回值说明:
    ***********************************/
    @PostMapping("changeUser")
    @Profile({"dev","test","stage"})
//    @UBA(module = "用户模块",action = "切换用户",channel = OperatorType.Button)
    public AjaxResult changeUser(String userId) {
        try {
            //判断是否存在用户
            String userInfo = currentUser.getUserInfo(userId);
            if (!StringUtils.isEmpty(userInfo)) {
                ContextHolder.runAs(userId);
                return success();
            } else {
                return error("不存在该用户");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**********************************
     * 用途说明: 获取人员所属上司消息
     * 参数说明 usersMap 20210908
     * 返回值说明:
     ***********************************/
    @PostMapping("getUsersBossInfo")
    public AjaxResult getUsersBossInfo(String userId) {
        try {
            //String userIds = usersMap.get("userIds").toString();
            List userslist = currentUser.getUsersByBoss(userId);
           // System.out.println("userslist---138:"+userslist);
            //String url =currentUser.getUsersByBoss(userId);
            return getObject(userslist);
            // return getObject(userIds);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
//    @PostMapping("getUsersByRole")
//    public AjaxResult getUsersByRole(String userId,String appcode) {
//        try {
//            //String userIds = usersMap.get("userIds").toString();
//            List userslist = currentUser.getUsersByRole(userId);
//            // System.out.println("userslist---138:"+userslist);
//            //String url =currentUser.getUsersByBoss(userId);
//            return getObject(userslist);
//            // return getObject(userIds);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return error(e.getMessage());
//        }
//    }
}